var ajaxUrl = require('../../utils/url.js');
var encrypt = require('../../utils/encrypt.js');
Component({
  externalClasses: ['my-class'],
  properties: {
    showChose: {
      type: Boolean,
      default: false
    }
  },
  data: {
    showProvince: true,
    showCity: false,
    showDistrict: false,
    showStreet: false,
    showVillage: false,
    province:{
      code: 5,
      name: ''
    },
    city: {
      code: 3,
      name: '',
    },
    county: {
      code: 57,
      name: ''
    },
    street: {
      code: 1,
      name: ''
    },
    village: {
      code: 1,
      name: ''
    },
    proviceList: [],
    cityList: [],
    countyList: [],
    streetList: [],
    villageList: []
  },
  attached () {
    this.fetchProvice()
    console.log(this.data.city)
  },
  methods: {
    fetchProvice () {
      const self = this
      let params ={
        level: 1
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.addressOption(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            self.setData({
              proviceList: res.data.data
            })
          }else{
            console.error(res.data.data)
          }
        }
      })
    },
    fetchCity (code) {
      const self = this
      let params = {
        parentCode: code
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.addressOption(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            self.setData({
              cityList: res.data.data
            })
          }else{
            console.error(res.data.data)
          }
        }
      })
    },
    fetchCounty(code){
      const self = this
      let params = {
        parentCode: code
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.addressOption(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            self.setData({
              countyList: res.data.data
            })
          }else{
            console.error(res.data.data)
          }
        }
      })
    },
    fetchStreet(code){
      const self = this
      let params = {
        parentCode: code
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.addressOption(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            self.setData({
              streetList: res.data.data
            })
          }else{
            console.error(res.data.data)
          }
        }
      })
    },
    fetchVillage(code){
      const self = this
      let params = {
        parentCode: code
      }
      params = encrypt.encrypt(params)
      wx.request({
        method: "POST",
        url: ajaxUrl.addressOption(),
        data: params,
        success: function(res){
          if(res.data.code === 200){
            self.setData({
              villageList: res.data.data
            })
          }else{
            console.error(res.data.data)
          }
        }
      })
    },
    closeAdd: function() {
      var myEventDetail = {} // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('close', myEventDetail, myEventOption)
    },
    getProvinceId: function(e) {
      const self = this
      let code = e.currentTarget.dataset.obj.code
      let name = e.currentTarget.dataset.obj.name
      let index = e.currentTarget.dataset.index
      let obj = {code: code, name: name}
      self.setData({
        province: obj,
        showProvince:false,
        showCity:true,
        showDistrict: false,
        showStreet: false,
        showVillage: false
      })

      self.fetchCity(self.data.province.code)
      console.log(self.data)
      
      // 点击选择当前
      this.data.proviceList.map( a => a.selected = false )
      console.log(index)
      this.data.proviceList[index].selected = true
    },
    provinceSelected: function() {
      const self = this
      self.setData({
        showProvince:true,
        showCity:false,
        showDistrict: false,
        showStreet: false,
        showVillage: false
      })
    },
    getCityId: function(e) {
      const self = this
      let code = e.currentTarget.dataset.obj.code
      let name = e.currentTarget.dataset.obj.name
      let index = e.currentTarget.dataset.index
      let obj = {code: code, name: name}
      self.setData({
        city: obj,
        showProvince:false,
        showCity:false,
        showDistrict: true,
        showStreet: false,
        showVillage: false
      })

      this.data.countyList = this.fetchCounty(this.data.city.code)
      this.data.cityList.map( a => a.selected = false )
      this.data.cityList[index].selected = true
    },
    citySelected: function() {
      const self = this
      self.setData({
        showProvince:false,
        showCity:true,
        showDistrict: false,
        showStreet: false,
        showVillage: false
      })
    },
    getDistrictId: function(e) {
      const self = this
      let code = e.currentTarget.dataset.obj.code
      let name = e.currentTarget.dataset.obj.name
      let index = e.currentTarget.dataset.index
      let obj = {code: code, name: name}
      self.setData({
        county: obj,
        showProvince:false,
        showCity:false,
        showDistrict: false,
        showStreet: true,
        showVillage: false
      })

      // this.street.name = false
      // this.village.name = false

      this.data.streetList = this.fetchStreet(this.data.county.code)
      this.data.countyList.map( a => a.selected = false )
      this.data.countyList[index].selected = true
    },
    districtSelected: function() {
      const self = this
      self.setData({
        showProvince:false,
        showCity: false,
        showDistrict: true,
        showStreet: false,
        showVillage: false
      })
    },
    getStreetId: function(e) {
      const self = this
      let code = e.currentTarget.dataset.obj.code
      let name = e.currentTarget.dataset.obj.name
      let index = e.currentTarget.dataset.index
      let obj = {code: code, name: name}
      self.setData({
        street: obj,
        showStreet: false,
        showVillage: true
      })

      // this.village.name = false

      this.data.villageList = this.fetchVillage(this.data.street.code)
      this.data.streetList.map( a => a.selected = false )
      this.data.streetList[index].selected = true
      // let data = {
      //   province: self.province,
      //   city: self.city,
      //   county: self.county,
      //   street: self.street
      // }
      // this.$emit('select', data)
    },
    streetSelected: function() {
      this.showProvince=false
      this.showCity=false
      this.showDistrict = false
      this.showStreet = true
      this.showVillage = false
    },
    getVillageId: function(e){
      const self = this
      let code = e.currentTarget.dataset.obj.code
      let name = e.currentTarget.dataset.obj.name
      let index = e.currentTarget.dataset.index
      let obj = {code: code, name: name}
      self.setData({
        village: obj,
      })

      this.data.villageList.map( a => a.selected = false )
      this.data.villageList[index].selected = true
      let data = {
        province: self.data.province,
        city: self.data.city,
        county: self.data.county,
        street: self.data.street,
        village: self.data.village
      }
      var myEventDetail = data
      this.triggerEvent('select', myEventDetail)
    },
    villageSelected: function() {
      this.showProvince=false
      this.showCity=false
      this.showDistrict = false
      this.showStreet = false
      this.showVillage = true
    }
  }
})